<template>
  <main class="login">
    <section class="bg-left" :style="{ background: `url(${bgUrl})` }"></section>
    <section class="bg-right">
      <h1 class="login-title">通用工时系统</h1>
      <section class="form-body">
        <h2>登录到您的账户</h2>
        <el-form
          ref="form"
          :model="form"
          label-position="top"
          label-width="80px"
          show-message
        >
          <el-form-item
            label="用户名或电子邮箱地址"
            prop="name"
            :rules="[
              {
                required: true,
                message: '请输入正确格式用户名',
                trigger: 'blur',
              },
              {
                type: 'string',
                message: '请输入正确格式用户名',
                trigger: ['blur', 'change'],
              },
            ]"
          >
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            :rules="[{ required: true, message: '密码不能为空' }]"
            prop="pwd"
          >
            <el-input
              v-model="form.pwd"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">登录</el-button>
            <el-button>使用第三方登录</el-button>
          </el-form-item>
        </el-form>
      </section>
    </section>
  </main>
</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      form: {
        name: '',
        pwd: ''
      },
      bgUrl: require(`../assets/images/bg0.jpg`),
      timerInterval: null
    }
  },
  mounted () {
    this.bgSwiper();
  },
  methods: {
    onSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('submit!');
          this.$http.post('/login', { data: this.form }).then(res => {
            console.log(res, '登录成功之后返回的结果');
            this.$store.commit('setToken', res?.data?.token);
            console.log(this.$store, 'pppp')
            localStorage.setItem('token', this.$store.getters.getToken);
            this.$router.push({ path: '/' });
            this.$message({
              message: '欢迎魂魂张',
              type: 'success'
            });
          }).catch(err => {
            console.log(err, '登录错误');
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    bgSwiper () {
      let index = 0;
      this.timerInterval = setInterval(() => {
        index < 7 ? this.bgUrl = require(`../assets/images/bg${index}.jpg`) : index = 0;
        // console.log(this.bgUrl)
        index++;
      }, 3000);
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: space-between;
  .bg-right {
    width: 65rem;
    height: 100%;
    position: relative;
    .login-title {
      font-size: 30px;
      color: #0066cc;
      position: absolute;
      left: 50%;
      top: 10%;
      transform: translateX(-50%);
      text-transform: uppercase;
      font-weight: 500;
      letter-spacing: 3px;
      line-height: 1.2em;
      white-space: normal;
      font-family: "Open Sans", Helvetica, Arial, sans-serif;
    }
    .form-body {
      width: 40rem;
      height: 40rem;
      padding: 20px 40px 30px 40px;
      box-shadow: 0 0 20px 5px rgb(160, 194, 205);
      border-top: 0.5rem solid #0066cc;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -35%);
      h2 {
        font-family: inherit;
        font-weight: 500;
        line-height: 1.1;
        font-size: 24px;
        width: 100%;
        text-align: center;
      }
      .el-button {
        width: 100%;
        margin-top: 2rem;
      }
      .el-button + .el-button,
      .el-checkbox.is-bordered + .el-checkbox.is-bordered {
        margin-left: 0;
      }
      ::v-deep .el-input__inner {
        -webkit-appearance: none;
        background-color: #fff;
        border-radius: 0px;
        border-bottom: 2px solid #5e74a6;
        box-sizing: border-box;
        color: #606266;
        display: inline-block;
        font-size: inherit;
        height: 40px;
        line-height: 40px;
        outline: 0;
        padding: 0 15px;
        transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
        width: 100%;
      }
    }
  }
  .bg-left {
    width: calc(100% - 650px);
    height: 100%;
    background: url("../assets/images/bg6.jpg") no-repeat;
    background-size: 100% 100%;
  }
}
</style>